package cn.kgc.vue.handler;

import cn.kgc.vue.commons.Const;
import cn.kgc.vue.commons.ResponseResult;
import cn.kgc.vue.exceptions.StudentNoException;
import com.auth0.jwt.exceptions.JWTVerificationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 课工场
 * @date 2024/7/2
 * @description  全局异常处理类
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(JWTVerificationException.class)
    public ResponseResult   jWTVerificationExceptionHandler( JWTVerificationException e){

        e.printStackTrace();
        return  ResponseResult.error().code(Const.UN_AUTH).message("token异常,请重新认证后访问");
    }


    // 数据库字段值重复异常
    @ExceptionHandler(DuplicateKeyException.class)
    public ResponseResult   duplicateKeyExceptionHandler( DuplicateKeyException e){
        e.printStackTrace();
        return  ResponseResult.error().message("添加的数据已存在,请检查!!!");
    }


    @ExceptionHandler(StudentNoException.class)
    public ResponseResult   studentNoExceptionHandler(StudentNoException e){
        e.printStackTrace();
        return  ResponseResult.error().message(e.getStudentNo()+"已经存在");
    }

    @ExceptionHandler(Exception.class)
    public ResponseResult   otherException( Exception e){

        e.printStackTrace();
        return  ResponseResult.error().message("服务器繁忙,请刷新重试!!!");
    }

    @ExceptionHandler(UnauthorizedException.class)
    public ResponseResult   unauthorizedException(UnauthorizedException e){

        e.printStackTrace();
        return  ResponseResult.error().message("没有权限请联系管理员");
    }

}
